<template>
  <div>
    <div class="form">
      <div class="search">
        <el-input placeholder="企业名称" v-model="companyName" class="search_info">
          <i slot="prefix" class="el-input__icon el-icon-search"></i>
        </el-input>
        <el-select v-model="departmentLable" placeholder="归属标签" class="search_info2">
          <el-option
            v-for="item in lableList"
            :key="item.id"
            :label="item.departmentName"
            :value="item.departmentName"
          ></el-option>
        </el-select>
        <el-select v-model="areaCode" placeholder="所属区县" class="search_info2">
          <el-option
            v-for="item in areaList"
            :key="item.areaId"
            :label="item.areaName"
            :value="item.areaId"
          ></el-option>
        </el-select>
        <div class="pos_right_btn">
          <el-button @click="reset()">重置</el-button>
          <el-button type="primary" @click="getList(1)" class="searchBtn">搜索</el-button>
          <el-button type="primary" @click="handleExport()" class="searchBtn">导出</el-button>
        </div>
      </div>
      <div>
        <el-table
          class="listTable"
          ref="multipleTable"
          v-loading="listLoading"
          :data="detailList"
          border
          fit
          highlight-current-row
          style="width: 100%;"
        >
          <el-table-column label="企业名称">
            <template slot-scope="{row}">
              <span>{{ row.companyName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="企业注册地址">
            <template slot-scope="{row}">
              <span>{{ row.address }}</span>
            </template>
          </el-table-column>
          <el-table-column label="统一社会信用编码">
            <template slot-scope="{row}">
              <span>{{ row.orgCode }}</span>
            </template>
          </el-table-column>
          <el-table-column label="所属区域">
            <template slot-scope="{row}">
              <span>{{ row.areaName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="归属部门名称">
            <template slot-scope="{row}">
              <span>{{ row.departmentName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="{row}">
              <div class="opt_table">
                <div class="opt_font" @click="comEdit(row.id)">修改</div>
                <div class="opt_font" @click="comDelete(row.id)">删除</div>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="footer">
          <div></div>
          <el-pagination
            :total="total"
            layout="total, sizes, prev, pager, next"
            :page-size="limit"
            :page-sizes="[10, 20, 50, 100]"
            @size-change="handleSizeChange"
            ref="Pagination"
            :current-page.sync="curPage"
            @current-change="getList($event)"
            class="sortStyle"
          />
        </div>
      </div>
    </div>
    <el-dialog title="企业信息修改" :visible.sync="editShow" center :modal-append-to-body="false" :close-on-click-modal="false">
		   <div class="inline">
         <div class="fonts">企业名称：</div><div><el-input v-model="editName" placeholder="请输入" readonly></el-input></div>
       </div>
       <div class="inline">
         <div class="fonts">企业注册地址：</div><div><el-input v-model="editAddress" placeholder="请输入"></el-input></div>
       </div>
       <div class="inline">
         <div class="fonts">统一社会信用编码：</div><div><el-input v-model="editCode" placeholder="请输入"></el-input></div>
       </div>
       <div class="inline">
         <div class="fonts">所属区域：</div><div>
           <!-- <el-input v-model="editArea"></el-input> -->
          <el-select v-model="editArea" placeholder="请选择" @change="selAreaChange">
            <el-option
              v-for="item in areaList"
              :key="item.areaId"
              :label="item.areaName"
              :value="item.areaId"
            ></el-option>
          </el-select>
         </div>
       </div>
       <div class="inline">
          <div class="fonts">归属部门名称：</div><div>
            <!-- <el-input v-model="editDepart"></el-input> -->
            <el-select v-model="editDepart" placeholder="请选择" @change="selChange" multiple>
              <el-option
                v-for="item in lableList"
                :key="item.id"
                :label="item.departmentName"
                :value="item.departmentName"
              ></el-option>
            </el-select>
          </div>
       </div>
       <div class="apply_btn">
          <el-button @click="editShow = false">取消</el-button>
          <el-button type="primary" @click="comUpdate()">修改</el-button>
       </div>
		</el-dialog>
  </div>
</template>

<script>
import {
  areaList,
  department,
  companyList,
  outExport,
  comDelete,
  comEdit,
  comUpdate
} from "@/api/system/user";
import { MessageBox } from "element-ui";
export default {
  components: {},
  data() {
    return {
      detailList: [],
      listLoading: false,
      listLoadings: false,
      total: 0,
      limit: 10,
      curPage: 0,
      isSel: true,
      totalList: [],
      startTime: '',
      endTime: '',
      areaCode: '',
      areaList: [],
      areaCodeAll: '',
      startTimeAll: '',
      endTimeAll: '',
      companyName: '',
      departmentLable: '',
      lableList: [],
      editShow: false,
      editName: '',
      editAddress: '',
      editCode: '',
      editArea: '',
      editDepart: [],
      editAreaId: '',
      editDepartId: '',
      editDepartment: '',
      editId: '',
    };
  },
  created() {

  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.getList(1)
      this.getDepartment()
      this.getArea()
    },
    selChange(data) {
      this.editDepartId = ''
      this.editDepartment = ''
      this.lableList.forEach(element1 => {
        data.forEach(element2 => {
          if (element2 == element1.departmentName) {
            this.editDepartId += element1.id + ','
            this.editDepartment += element1.departmentName + ',' 
          }
        })
      })
      this.editDepartId = this.editDepartId.substring(0, this.editDepartId.length - 1)
      this.editDepartment = this.editDepartment.substring(0, this.editDepartment.length - 1)
    },
    selAreaChange(data) {
      this.editAreaId = data
    },
    comUpdate() {
      let params = {
        id: this.editId,
        address: this.editAddress,
        orgCode: this.editCode,
        areaId: this.editAreaId,
        department: this.editDepartId,
        departmentName: this.editDepartment
      }
      comUpdate(params).then(res => {
        if (res.code == '200') {
          this.editShow = false
          this.getList(1)
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },

    comEdit(id) {
      this.editId = ''
      this.editName = ''
      this.editAddress = ''
      this.editCode = ''
      this.editArea = ''
      this.editDepart = []
      comEdit(id).then(res => {
        if (res.code == '200') {
          this.editShow = true
          this.editId = id
          this.editName = res.data.companyName
          this.editAddress = res.data.address
          this.editCode = res.data.orgCode
          this.areaList.forEach(element => {
            if (res.data.areaId == element.areaId) {
              this.editArea = element.areaName
            }
          });
          this.lableList.forEach(element1 => {
            if (res.data.department.split(',').length > 0) {
              res.data.department.split(',').forEach(element2 => {
                if (element2 == element1.id) {
                  this.editDepart.push(element1.departmentName)
                }
              })
            } else {
              if (res.data.department == element1.id) {
                this.editDepart.push(element1.departmentName)
              }
            }
            
          })
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    comDelete(id) {
      this.$confirm('确定该企业吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        comDelete(id).then(res => {
          if (res.code == '200') {
            this.$message({ message: res.msg, type: "success" });
            this.getList(1)
          } else {
            this.$message({ message: res.msg, type: "error" });
          }
        });
      })
     
    },
    /** 导出按钮操作 */
    handleExport() {
      let params = {
        departmentName: this.departmentLable,
        areaId: this.areaCode,
        companyName: this.companyName
      }
      this.$confirm("是否确认导出企业信息?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return outExport(params);
        })
        .then(response => {
          this.download(response.msg);
        });
    },
    getArea() {
      areaList().then(res => {
        if (res.code == '200') {
          this.areaList = res.rows
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    //列表
    getList(page) {
      this.listLoading = true
      let params = {
        pageNum: page,
        pageSize: this.limit,
        companyName: this.companyName,
        departmentName: this.departmentLable,
        areaId	: this.areaCode
      }
      companyList(params).then(res => {
        this.listLoading = false
        if (res.code == '200') {
          this.detailList = res.rows
          this.total = res.total
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    //归属部门标签
    getDepartment() {
      department().then(res => {
        if (res.code == '200') {
          this.lableList = res.rows
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    handleSizeChange(val) {
      this.limit = val;
      this.getList(1);
    },
    selClick(tab) {
      this.isSel = tab
    },
    reset() {
      this.areaCode = ''
      this.departmentLable = ''
      this.companyName = ''
    }
  }
};
</script>
<style scoped>
.form {
  padding-left: 26px;
  padding-right: 24px;
  padding-top: 24px;
  background: #fff;
}
.search {
  display: flex;
  position: relative;
}
.search1 {
  display: flex;
}
.search_info {
  width: 120px;
  height: 40px;
  margin-right: 16px;
}
.search_info1 {
  width: 200px;
  height: 40px;
  margin-right: 16px;
}
.search_info2 {
  width: 140px;
  height: 40px;
  margin-right: 16px;
}
.searchBtn {
  margin-left: 16px;
  height: 36px;
}
.sortStyle {
  height: 19px;
  float: right;
  /* right: 24px; */
  margin-top: 20px;
  margin-bottom: 30px;
}
.pos_right_btn {
  position: absolute;
  right: 0;
}
.opt_font {
  cursor: pointer;
  color: #2e82ff;
}
.detail_title {
  font-size: 16px;
  font-weight: 400;
  color: #212121;
  line-height: 22px;
}
.detail_title span {
  padding-left: 4px;
  background: #1e88e5;
  margin-right: 12px;
}
.row {
  display: flex;
  /* padding-bottom: 12px; */
}
.detail_info {
  padding-top: 16px;
}
.info_left {
  display: flex;
}
.info_right {
  display: flex;
  /* padding-left: 202px; */
}
.info_left_title {
  font-size: 14px;
  font-weight: 400;
  color: #616161;
  line-height: 20px;
  width: 256px;
  /* margin-right: 16px; */
  text-align: center;
  min-height: 47px;
  background: #f5f7fa;
  box-shadow: inset 0 -1px 0 0 #dcdfe6;
}
.info_left_title .font {
  position: relative;
  top: 14px;
}
.info_left_content {
  font-size: 14px;
  font-weight: 400;
  color: #212121;
  line-height: 20px;
  width: 256px;
  /* border: 1px solid #DCDFE6; */
  box-shadow: inset -1px -1px 0 0 #dcdfe6;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  /* position: relative;
    top: 10px; */
}
.info_left_contents {
  font-size: 14px;
  font-weight: 400;
  color: #212121;
  line-height: 20px;
  width: 769px;
  /* border: 1px solid #DCDFE6; */
  box-shadow: inset -1px -1px 0 0 #dcdfe6;
  text-align: left;
}
.pos_top {
  padding-top: 12px;
}
.top_more {
  margin-top: 8px;
}
.detailTable {
  margin-top: 16px;
}
.opt_font {
  color: #1e88e5;
  cursor: pointer;
}
.page_pos {
  text-align: right;
  padding-top: 10px;
}
.backBtn {
  text-align: center;
  margin-top: 10px;
}
/deep/ .el-dialog {
  display: flex;
  flex-direction: column;
  margin: 0 !important;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-height: calc(100% -30px);
  /* max-width: ; */
}
/deep/ .el-dialog .el-dialog__body {
  flex: 1;
  height: 600px;
  overflow-y: auto;
  padding-top: 0;
}
.tab {
  display: flex;
  position: absolute;
  right: 55px;
  top: 14px;
}
.tab_border {
  border: 1px solid #409eff;
  color: #409eff;
  padding: 10px 12px;
  font-size: 14px;
  margin-right: 16px;
}
.change {
  border: 1px solid #f56c6c;
  color: #f56c6c;
}
.listTable {
  margin-top: 17px;
}
/deep/ .search_info .el-input__inner {
  width: 120px;
}
/deep/ .search_info1 .el-input__inner {
  width: 200px;
}
/deep/ .search_info2 .el-input__inner {
  width: 140px;
}
.date_picker /deep/ .el-input__inner {
  width: 240px;
}
.opt_table {
  display: flex;
  justify-content: space-around;
}
.footer {
  display: flex;
  justify-content: space-between;
}
.footer_btn {
  position: relative;
  top: 20px;
}
.opt_disable {
  color: #909399;
}
.tabs {
  padding-bottom: 10px;
  cursor: pointer;
}
.pad_right {
  margin-right: 10px;
}
.sel {
  border-bottom: #1e88e5 solid 2px;
}
.mar_left {
  margin-left:20px;
}
.inline {
  display: flex;
  justify-content: center;
  padding-bottom: 20px;
}
.inline /deep/ .el-input__inner {
  width: 340px;
}
.fonts {
  width:160px;
  text-align: right;
  position: relative;
  top:12px;
  padding-right: 30px;
}
.apply_btn {
  display: flex;
  justify-content:center; 
}
</style>
